package com.graduation.vaccine.controller.web;

import com.graduation.vaccine.service.UserService;
import com.graduation.vaccine.service.VaccineCatalogService;
import com.graduation.vaccine.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author:CaiShuangLian
 * @FileName:
 * @Date:Created in  2022/5/1 15:44
 * @Version:
 * @Description:TODO
 */
@RestController
@RequestMapping("/web/catalog")
@Api(tags = "web——疫苗种类管理")
public class WebCatalogController {
    @Autowired
    private UserService userService;
    @Autowired
    private VaccineCatalogService vaccineCatalogService;

    @ApiOperation("5/01-01:获取疫苗目录列表")
    @PostMapping("/list")
    @ResponseBody
    public RespBean getList(@RequestBody String ticket, HttpServletRequest request, HttpServletResponse response){
        if(ticket!=null && !ticket.isEmpty()){
            //根据cookie获取用户信息
            UserVo userVo=userService.getUserByCookie(ticket,request,response);
            if(userVo!=null && (userVo.getAction().equals("system_administrator")||userVo.getAction().equals("organization_administrator"))||userVo.getAction().equals("organization_member")){
                return vaccineCatalogService.sysGetList();
            }

            return RespBean.error(RespBeanEnum.NO_OPERATION_PERMISSION,"非管理员，无法查看");
        }
        return RespBean.error(RespBeanEnum.ILLEGAL_OPERATION);
    }

    @ApiOperation("5/01-01:添加疫苗目录（仅系统管理员有权限）")
    @PostMapping("/add")
    @ResponseBody
    public RespBean addOne(@RequestBody AddVaccineCatalogVo addVaccineCatalogVo, HttpServletRequest request, HttpServletResponse response){
        String ticket=addVaccineCatalogVo.getTicket();
        if(ticket!=null && !ticket.isEmpty()){
            //根据cookie获取用户信息
            UserVo userVo=userService.getUserByCookie(ticket,request,response);
            if(userVo!=null && (userVo.getAction().equals("system_administrator"))){
                return vaccineCatalogService.addOne(addVaccineCatalogVo);
            }

            return RespBean.error(RespBeanEnum.NO_OPERATION_PERMISSION,"非管理员，无法添加");
        }
        return RespBean.error(RespBeanEnum.ILLEGAL_OPERATION);
    }

    @ApiOperation("5/01-01:修改疫苗目录")
    @PostMapping("/update")
    @ResponseBody
    public RespBean updateCatalog(@RequestBody UpdateCatalogVo updateCatalogVo,HttpServletRequest request, HttpServletResponse response){
        String ticket=updateCatalogVo.getTicket();
        if(ticket!=null && !ticket.isEmpty()){
            //根据cookie获取用户信息
            UserVo userVo=userService.getUserByCookie(ticket,request,response);
            if(userVo!=null && (userVo.getAction().equals("system_administrator"))){
                return vaccineCatalogService.updateCatalog(updateCatalogVo);
            }

            return RespBean.error(RespBeanEnum.NO_OPERATION_PERMISSION,"非管理员，无法修改");
        }
        return RespBean.error(RespBeanEnum.ILLEGAL_OPERATION);
    }

    @ApiOperation("5/01-02:种类下拉框")
    @ResponseBody
    @PostMapping("/dropDownBox")
    public RespBean getDropDownBox(@RequestBody String ticket, HttpServletRequest request, HttpServletResponse response){
        if(ticket!=null && !ticket.isEmpty()){
            //根据cookie获取用户信息
            UserVo userVo=userService.getUserByCookie(ticket,request,response);
            if(userVo!=null && (userVo.getAction().equals("system_administrator"))){
                return vaccineCatalogService.getDropDownBox();
            }

            return RespBean.error(RespBeanEnum.NO_OPERATION_PERMISSION,"非管理员，无法添加");
        }
        return RespBean.error(RespBeanEnum.ILLEGAL_OPERATION);
    }
}
